using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CameraControl : MonoBehaviour {
	
	public Transform playerTransform;	
	public enum CamState { FocusPlayer, FreeCam , FocusBattle};
	public CamState camState = CamState.FocusPlayer;

	Vector2 lastPosition;
	public float camSpeedMultiplier;
	public float originalCamSpeedMultiplier = 0.003f;
	public float camLerpMultiplier = 4.0f;
	public float zoomSpeedMultiplier = 0.003f;
	public float maxOrthographicSize = 2.0f;
	public float minOrthographicSize = 0.8f;
	float defaultOrthoSize = 1.0f;
	public float camSpeedDiminishFactor = 0.02f;
	
	bool bPreventCamTranslateForMoment;
	public float camSpeedDiminish;
	public bool isUIButtonPressed = false;
	public bool bProceedingDestMarkZoom;
	public bool bBackToOriginalZoom;
	float timeDelay = 0;
	public float ProceedingDestMarkZoomSpeed = 1.0f;
	float m_fProceedingDestMarkZoomLimit = 2.0f;
	public bool isPreventChangeFocus;
	public bool bForceFreeCam;
	public bool bFreeCamOnlyMyTurn;
	public bool bFreeCamOnlyZoom;
	bool multiTouch = false;
	float prevDistSq;
	int lastTouchCount = 0;
	bool buttonUp = true;
	public float doubleTouchGapTime = 0.3f;
	public bool bBoundZoom = true;
	
	public bool bBoundPosition = false;
	public float boundRect_left = -4.4f;
	public float boundRect_right = 3.3f;
	public float boundRect_bottom = 0.2f;
	public float boundRect_top = 5.7f;
	
	public List<Rect> ignoreTouchRectList = new List<Rect>();
	
	bool bInstantFocus = false;
	public int fingerCount = 0;
	float lastTouchTime;
	public Vector3 moveDelta;
	
	public bool bIgnoreTimeScale;
	public float boundSpringStrength = 6.0f;
	public float deltaTime { get { return bIgnoreTimeScale ? RealTime.deltaTime : Time.deltaTime;    } } 
	
	public float eventZoomInValue = 0.8f;
	public iTween.EaseType eventZoomInEaseType = iTween.EaseType.easeOutSine;
	public iTween.EaseType eventZoomOutEaseType = iTween.EaseType.easeOutSine;
	public float eventZoomInTime = 0.5f;
	public float eventZoomOutTime = 0.5f;
	public bool bEventZoomIn;
	public bool bEventZoomOut;
	public bool bUseEventZoom;
	public bool bEventZoomOnlyMyTurn;
	public bool bEventZoomWhenBattleWithMe;
	public bool bEventZoomOnlyBattle;
	public bool bUseCamLerp = true;
	float m_fLastPress;
	int nDoubleTouchZoom;
	float [] DoubleTouchZoomOrtho;
	//BoardPerson personToCheckDestMark;
	//Camera backCamera;
	GameObject sky_bg;
	Vector3 sky_bg_originalScale;

	void Awake()
	{
		DoubleTouchZoomOrtho = new float[3];
		DoubleTouchZoomOrtho[0] = 1;
		DoubleTouchZoomOrtho[1] = 1.5f;
		DoubleTouchZoomOrtho[2] = 2;

		//Transform backTrans = gameObject.transform.FindChild("backcamera");
		//backCamera = backTrans.gameObject.GetComponent<Camera>();

	}

	public void InitSkyBgScale(GameObject _sky_bg)
	{
		sky_bg = _sky_bg;//transform.FindChild("sky_bg").gameObject;
		sky_bg_originalScale = sky_bg.transform.localScale;
	}

	// Use this for initialization
	void Start () {
		camSpeedMultiplier = originalCamSpeedMultiplier;
		playerTransform = null;
		glog.debug ("Camera.main.orthographicSize="+Camera.main.orthographicSize);
		glog.debug ("Camera.main.aspect="+Camera.main.aspect);
		glog.debug ("Camera.pixel w="+Camera.main.pixelWidth+"  h="+Camera.main.pixelHeight);
		ignoreTouchRectList.Add( new Rect(10, Screen.height - 10 - Screen.height / 10, Screen.width / 10, Screen.height / 10) );
		glog.debug ("ignore touch rect= "+ignoreTouchRectList[0]);
	}

	public void SetCameraFocus(Transform playerT)
	{
		Vector3 scrPos = Camera.main.WorldToScreenPoint(playerT.position);
		glog.debug ("player scrPos = "+ scrPos.ToString());
		if(scrPos.x < 0 || scrPos.x > Camera.main.pixelWidth || scrPos.y <0 || scrPos.y > Camera.main.pixelHeight)
			bInstantFocus = true;
		playerTransform = playerT;
		camState = CamState.FocusPlayer;
	}

	public void SetBattleCameraFocus(bool bActivate)
	{
		if(bActivate)
		{
			camState = CamState.FocusBattle;
		}
		else
		{
			SetCameraFocus(playerTransform);
		}
	}
	
	public void ResetZoom()
	{
		Camera.main.orthographicSize = 1;
	}
	
	public void SetCameraFocusMe()
	{
		StartCoroutine("CoSetCameraFocusMe");
	}
	
	IEnumerator CoSetCameraFocusMe()
	{
		if(bForceFreeCam)
		{
			bForceFreeCam = false;
			SetCameraFocus( BoardManager.Instance.boardPerson_me.gameObject.transform);
			yield return new WaitForSeconds(1.0f);
			bForceFreeCam = true;
		}
		else
			SetCameraFocus( BoardManager.Instance.boardPerson_me.gameObject.transform);
	}
	
	public void ChangeInstant()
	{
		bInstantFocus = true;
	}
	
	public void SetBoundary(float l, float r, float b, float t)
	{
		boundRect_left = l;
		boundRect_right = r;
		boundRect_bottom = b;
		boundRect_top = t;
	}
	
	public void PressedUIButton(bool bPressed)
	{
		isUIButtonPressed = bPressed;
	}
	
	
	public void SetCameraTest(CameraTestMenu testMenu)
	{
		if(testMenu== CameraTestMenu.FreeCam1)
		{
			bForceFreeCam = true;
			bFreeCamOnlyMyTurn = false;
			bFreeCamOnlyZoom = false;
		}
		else if(testMenu== CameraTestMenu.FreeCam2)
		{
			bForceFreeCam = true;
			bFreeCamOnlyMyTurn = true;
			bFreeCamOnlyZoom = false;
		}
		else if(testMenu== CameraTestMenu.FreeCam3)
		{
			bForceFreeCam = false;
			bFreeCamOnlyMyTurn = false;
			bFreeCamOnlyZoom = true;
		}
		else if(testMenu== CameraTestMenu.NoFreeCam)
		{
			bForceFreeCam = false;
			bFreeCamOnlyMyTurn = false;
			bFreeCamOnlyZoom = false;
		}
		else if(testMenu == CameraTestMenu.NoZoomEffect)
		{
			bUseEventZoom = false;
		}
		else if(testMenu == CameraTestMenu.ZoomEffect1)
		{
			bUseEventZoom = true;
			bEventZoomOnlyMyTurn = false;
			bEventZoomOnlyBattle = false;
		}
		else if(testMenu == CameraTestMenu.ZoomEffect2)
		{
			bUseEventZoom = true;
			bEventZoomOnlyMyTurn = true;
			bEventZoomOnlyBattle = false;
		}
		else if(testMenu == CameraTestMenu.ZoomEffect3)
		{
			bUseEventZoom = true;
			bEventZoomOnlyMyTurn = false;
			bEventZoomOnlyBattle = true;
		}
		else if(testMenu == CameraTestMenu.ZoomSpeed1)
		{
			eventZoomInTime = 0.5f;
			eventZoomOutTime = 0.5f;			
		}
		else if(testMenu == CameraTestMenu.ZoomSpeed2)
		{
			eventZoomInTime = 0.0f;
			eventZoomOutTime = 0.0f;						
		}
		else if(testMenu == CameraTestMenu.ZoomSpeed3)
		{
			eventZoomInTime = 0.25f;
			eventZoomOutTime = 0.25f;			
		}
		else if(testMenu == CameraTestMenu.InstantFocus)
		{
			bUseCamLerp = !bUseCamLerp;
		}
	}
	
	bool IsFreeCam()
	{
		if(bForceFreeCam)
		{
			if(bFreeCamOnlyMyTurn)
			{
				if( StageManagerWrapper.m_isMyTurn)
					return true;
			}
			else
				return true;
		}
		
		return false;
	}
	
	public void SetEventZoomIn(float restoreTime)
	{
		StartCoroutine( CoSetEventZoomIn(restoreTime) );
	}
	
	IEnumerator CoSetEventZoomIn(float restoreTime)
	{
		EventZoomIn();
		yield return new WaitForSeconds(restoreTime);
		EventZoomOut();
	}

	public void BackToDefaultOrtho()
	{
		if(Camera.main.orthographicSize > defaultOrthoSize)
		{
			Hashtable ht = iTween.Hash("from",Camera.main.orthographicSize, "to", defaultOrthoSize ,"time", eventZoomInTime, "easetype",eventZoomInEaseType,"onupdate","UpdateBackToDefaultOrtho" );
			iTween.ValueTo(gameObject,ht);	
			//Camera.main.orthographicSize = defaultOrthoSize;
			//backCamera.orthographicSize = Camera.main.orthographicSize;
			if(sky_bg!=null)
				iTween.ScaleTo( sky_bg , iTween.Hash("time", eventZoomInTime, "x", sky_bg_originalScale.x, "y",sky_bg_originalScale.y , "islocal", true, "easetype",eventZoomInEaseType)  );
		}
	}

	void UpdateBackToDefaultOrtho(float newValue)
	{
		Camera.main.orthographicSize = newValue;
		//backCamera.orthographicSize = Camera.main.orthographicSize;
	}

	public bool EventZoomIn()
	{
		if(!bUseEventZoom)
			return false;
		if(bEventZoomOnlyMyTurn && !StageManagerWrapper.m_isMyTurn )
		{
			if(bEventZoomWhenBattleWithMe )//BattleManager.Instance.IsBattleWithMe()
			{
				if(BattleManager.Instance.IsDuringBattleWithMe() )
				{
					// show zoom effect
				}
				else
					return false;
			}
			else	
				return false;
		}
		
		if(bEventZoomOnlyBattle && !BattleManager.Instance.isDuringBattle)
			return false;
		
		iTween itween = GetComponent<iTween>();
		if(itween!=null && itween.isRunning)
		{
			iTween.StopByName(gameObject, "zoomOut");
			glog.debug("stop itween zoomOut");
		}
		//Debug.LogError("Zoom In");
		
//		Camera.main.orthographicSize = eventZoomInValue;
		//Debug.LogError("EventZoomIn");
		Hashtable ht = iTween.Hash("from",Camera.main.orthographicSize, "to", eventZoomInValue ,"time",eventZoomInTime,"easetype",eventZoomInEaseType,  "onupdate","ChangeUpdateZoomIn");
		iTween.ValueTo(gameObject,ht);		
		return true;
	}
	
	public void EventZoomOut()
	{
		if(!bUseEventZoom)
			return;
		if(bEventZoomOnlyMyTurn && !StageManagerWrapper.m_isMyTurn)
		{
			if(bEventZoomWhenBattleWithMe )//BattleManager.Instance.IsBattleWithMe()
			{
				if(BattleManager.Instance.IsDuringBattleWithMe() )
				{
					// show zoom effect
				}
				else
					return;
			}
			else	
				return;			
		}
		
		if(bEventZoomOnlyBattle && !BattleManager.Instance.isDuringBattle)
			return;
		
//		Camera.main.orthographicSize = 1;
		//Debug.LogError("EventZoomOut");

		if(Camera.main.orthographicSize > defaultOrthoSize)// zoom out only when necessary( only when zoomed in state)
			return;
		if(bProceedingDestMarkZoom)
			return;
		Hashtable ht = iTween.Hash("name","zoomOut", "from",Camera.main.orthographicSize, "to", defaultOrthoSize ,"time",eventZoomOutTime,"easetype",eventZoomOutEaseType,  "onupdate","ChangeUpdateZoomOut");
		iTween.ValueTo(gameObject,ht);		
	}
	
	void ChangeUpdateZoomIn(float newValue)
	{
	//	glog.debug(gameObject.name+ "  newBalue="+newValue);
		if(bProceedingDestMarkZoom)
			return;
		if(bBackToOriginalZoom)
			bBackToOriginalZoom = false;
		Camera.main.orthographicSize = newValue;
		//backCamera.orthographicSize = newValue;
		SetSkyBgScale();
	}	
	void ChangeUpdateZoomOut(float newValue)
	{
	//	glog.debug(gameObject.name+ "  newBalue="+newValue);
		if(bProceedingDestMarkZoom)
			return;
		if(bBackToOriginalZoom)
			bBackToOriginalZoom = false;
		Camera.main.orthographicSize = newValue;
		//backCamera.orthographicSize = newValue;
		SetSkyBgScale();
	}

	public void updateValueCallback( float val, object hash ){
		// Hashtable h = hash as Hashtable;
		// Debug.Log("message:"+h["message"]+" val:"+val);
		Camera.main.orthographicSize = val;
		//backCamera.orthographicSize = val;
		SetSkyBgScale();
	}

	void SetSkyBgScale()
	{
		if(sky_bg==null)
			return;
		float ratio = Camera.main.orthographicSize / defaultOrthoSize;
		sky_bg.transform.localScale = new Vector3( sky_bg_originalScale.x * ratio, sky_bg_originalScale.y * ratio, 1);
	}

	public void DoDoubleTouch()
	{
		if(bProceedingDestMarkZoom)
			return;
		if(bBackToOriginalZoom)
			bBackToOriginalZoom = false;
		if(isUIButtonPressed)
			return;
		glog.debug("camera Double touch");
		nDoubleTouchZoom++;
		if(nDoubleTouchZoom>2)
			nDoubleTouchZoom=0;
		
		LeanTween.value( gameObject, updateValueCallback, Camera.main.orthographicSize, DoubleTouchZoomOrtho[nDoubleTouchZoom], 0.2f);
	}

	public void CheckDestMarkWithinCamera()
	{
		if(bProceedingDestMarkZoom)
			return;
		//Debug.LogError("CheckDestMarkWithinCamera");
		//personToCheckDestMark = person;
		if(!IsDestMarkWithinCamera())
			bProceedingDestMarkZoom = true;
	}

	bool IsDestMarkWithinCamera()
	{
		foreach( BoardTile tile in BoardManager.Instance.destinationTiles)
		{
			Vector3 pos = Camera.main.WorldToViewportPoint( tile.transform.position);
			float tolerence = 0.05f;
			if(pos.x<tolerence || pos.x>1-tolerence || pos.y<tolerence || pos.y>1-tolerence)
			{
				return false;
			}
		}

		return true;
	}

	// Update is called once per frame
	void LateUpdate () {
		
		if(bEventZoomIn)
		{
			bEventZoomIn=false;
			EventZoomIn();
		}
		
		if(bEventZoomOut)
		{
			bEventZoomOut=false;
			EventZoomOut();
		}		

		if( Input.touchCount>=1)
		{
			if(Input.GetTouch(0).phase == TouchPhase.Began)
			{
				if( Time.timeSinceLevelLoad - lastTouchTime < doubleTouchGapTime)
				{
					//glog.debug(string.Format( "touch position {0}",Input.GetTouch(0).position) );
					DoDoubleTouch();
				}
				lastTouchTime = Time.timeSinceLevelLoad;
			}
		}

#if UNITY_EDITOR || UNITY_WEBPLAYER
		if( Input.GetMouseButtonDown(0) )
		{
			if( Time.timeSinceLevelLoad - lastTouchTime < doubleTouchGapTime)
			{
				glog.debug(string.Format( "touch position {0}",Input.mousePosition) );
				DoDoubleTouch();
			}
			lastTouchTime = Time.timeSinceLevelLoad;
		}
#endif


		if(Input.GetKeyDown(KeyCode.C) )
			DoDoubleTouch();


		if(bProceedingDestMarkZoom)
		{
			Camera.main.orthographicSize += ProceedingDestMarkZoomSpeed * Time.deltaTime;
			if(Camera.main.orthographicSize>m_fProceedingDestMarkZoomLimit)
				Camera.main.orthographicSize=m_fProceedingDestMarkZoomLimit;
			//backCamera.orthographicSize = Camera.main.orthographicSize;
			SetSkyBgScale();
			if(IsDestMarkWithinCamera() || Camera.main.orthographicSize==m_fProceedingDestMarkZoomLimit)
			{
				bProceedingDestMarkZoom = false;
				bBackToOriginalZoom = true;
				timeDelay = 0;
			}
		}

		if(bBackToOriginalZoom)
		{
			timeDelay += Time.deltaTime;
			if(timeDelay>0.3f)
			{
				if(IsDestMarkWithinCamera())
					Camera.main.orthographicSize -= 0.5f * Time.deltaTime;
				if(Camera.main.orthographicSize <= defaultOrthoSize)
				{
					Camera.main.orthographicSize = defaultOrthoSize;
					bBackToOriginalZoom = false;
				}
				//backCamera.orthographicSize = Camera.main.orthographicSize;
				SetSkyBgScale();
			}
		}

		//if(IsTouched() && !isUIButtonPressed)
		bool isTouched = IsTouched();


		if( isTouched && !isUIButtonPressed && IsFreeCam())
		{
			camState = CamState.FreeCam;
			if(IsMultiTouch())
			{
				if(IsMultiTouchMoved())
				{
					float currentDistSq = DistBetweenTouches();
					if(multiTouch == false)
					{
						multiTouch = true;
						prevDistSq = currentDistSq;
					}
					float zoomOffset = ZoomOffset(currentDistSq);
					if(Camera.main.orthographic)
					{
						if(Mathf.Abs(zoomOffset) > 0)
							Camera.main.orthographicSize += zoomOffset * zoomSpeedMultiplier;
						
						if(bBoundZoom)
						{
							if(Camera.main.orthographicSize> maxOrthographicSize)
								Camera.main.orthographicSize = maxOrthographicSize;
							if(Camera.main.orthographicSize<minOrthographicSize)
								Camera.main.orthographicSize = minOrthographicSize;
						}
					}
					else
					{
						if(Mathf.Abs(zoomOffset) > 0)
						{
							float z_offset = zoomOffset * zoomSpeedMultiplier;
							Vector3 newCamPos = transform.position;
							newCamPos.z += z_offset;
							transform.position = newCamPos;
						}
									
					}
					prevDistSq = currentDistSq;
				}
				lastTouchCount = 2;
				camSpeedDiminish = 0;
			}
			else
			{
				Vector2 touchPos = GetTouchPos();
				multiTouch = false;
				if(buttonUp)//for mouse
				{
					lastPosition = touchPos;
					buttonUp = false;
				}			
				if(IsTouchMoved() && lastTouchCount ==1)
				{
					float moveDelta_x = touchPos.x - lastPosition.x;
					float moveDelta_y = touchPos.y - lastPosition.y;
					if( Mathf.Abs(moveDelta_x) > 0.5f || Mathf.Abs(moveDelta_y) > 0.5f)
					{
						//glog.debug ("mouse x "+moveDelta_x);
						moveDelta = new Vector3(-moveDelta_x,-moveDelta_y,0);
						transform.Translate(moveDelta * camSpeedMultiplier);
					}
					//if( Mathf.Abs(moveDelta_y) > 0.5f)
					//{
						//glog.debug ("mouse Y  "+moveDelta_y);
					//	moveDelta = new Vector3(0,-moveDelta_y, 0);
					//	transform.Translate(moveDelta * camSpeedMultiplier);				
					//}			
					
					
				}
				
				lastPosition = touchPos;
				
				camSpeedDiminish = camSpeedMultiplier;
				if(lastTouchCount == 2)
				{
					StartCoroutine("PreventCamTranslateForMoment");
				}
				lastTouchCount = 1;
				
			}
		}
		else
		{
			
			multiTouch = false;
			buttonUp = true;
			if( camState == CamState.FocusPlayer)
			{
				if(playerTransform)
				{
					moveDelta = new Vector3(0,0,0);
					Vector3 newPos = playerTransform.position;
					newPos.z = transform.position.z;
					
					if(bInstantFocus) {
						bInstantFocus = false;
						transform.position = newPos;
					}
					else
					{
						//NGUIglog.debug("myTest "+transform.position);
						if(bUseCamLerp)
							transform.position = Vector3.Lerp(transform.position, newPos, camLerpMultiplier * Time.deltaTime );//camLerpMultiplier * Time.deltaTime);// 1 - Mathf.Exp( -20 * Time.deltaTime )
							//transform.position = Vector3.Lerp(transform.position, newPos, 1 - Mathf.Exp( -camLerpMultiplier * deltaTime ));//camLerpMultiplier * Time.deltaTime);// 1 - Mathf.Exp( -20 * Time.deltaTime )
						else
							transform.position = newPos;
						//iTween.MoveTo(gameObject, iTween.Hash("position", newPos/*, "easetype", "linear", "speed", 3.0f */,"time",1.0f) );
					}
					
				}
			}
			else if( camState == CamState.FocusBattle)
			{
				Vector3 newPos = (BattleManager.Instance.attacker.transform.position + BattleManager.Instance.defender.transform.position) * 0.5f;
				newPos.z = transform.position.z;
				if(bInstantFocus) {
					bInstantFocus = false;
					transform.position = newPos;
				}
				else
					transform.position = Vector3.Lerp(transform.position, newPos, camLerpMultiplier * Time.deltaTime );
			}
			else if( camState == CamState.FreeCam)
			{
				if(bPreventCamTranslateForMoment)
					camSpeedDiminish = 0;
				if(camSpeedDiminish>0)
				{
					camSpeedDiminish -= deltaTime * camSpeedDiminishFactor;
					if(camSpeedDiminish<0)
						camSpeedDiminish = 0;
					transform.Translate(moveDelta * camSpeedDiminish); 
				}
			}
		}		

		
		if(bFreeCamOnlyZoom)
		{
			if(IsTouched())
			{
				if(IsMultiTouch())
				{
					if(IsMultiTouchMoved())
					{
						float currentDistSq = DistBetweenTouches();
						if(multiTouch == false)
						{
							multiTouch = true;
							prevDistSq = currentDistSq;
						}
						float zoomOffset = ZoomOffset(currentDistSq);
						if(Camera.main.orthographic)
						{
							if(Mathf.Abs(zoomOffset) > 0)
								Camera.main.orthographicSize += zoomOffset * zoomSpeedMultiplier;
							
							if(bBoundZoom)
							{
								if(Camera.main.orthographicSize> maxOrthographicSize)
									Camera.main.orthographicSize = maxOrthographicSize;
								if(Camera.main.orthographicSize<minOrthographicSize)
									Camera.main.orthographicSize = minOrthographicSize;
							}
						}
						prevDistSq = currentDistSq;
					}
					lastTouchCount = 2;
					camSpeedDiminish = 0;
				}	
			}
			
		}
		
		
		if(bBoundPosition)
			BoundCamPosition();
	}
	
	IEnumerator PreventCamTranslateForMoment()
	{
		bPreventCamTranslateForMoment = true;
		yield return new WaitForSeconds(0.5f);
		bPreventCamTranslateForMoment = false;
	}
	
	void BoundCamPosition()
	{
		
		//Time.timeScale = 0;
		Vector3 camPos = transform.position;
		//float halfWidth = Camera.main.aspect * Camera.main.orthographicSize;
		float boundOffset_y = Camera.main.orthographicSize - 1;
		float boundOffset_x = boundOffset_y * Camera.main.aspect;
		if(camPos.x < boundRect_left + boundOffset_x)
		{
			//camPos.x = boundRect_left + boundOffset_x;
			if(Input.GetMouseButton(0) || Input.touchCount > 0)
			{
				if(camPos.x < boundRect_left + boundOffset_x )//- 0.5f)
					camPos.x = boundRect_left + boundOffset_x;//- 0.5f;
			}
			else
				camPos.x = NGUIMath.SpringLerp(camPos.x, boundRect_left + boundOffset_x, boundSpringStrength, deltaTime);
			
		}
		if(camPos.x  > boundRect_right - boundOffset_x)
		{
			//camPos.x = boundRect_right - boundOffset_x;
			if(Input.GetMouseButton(0) || Input.touchCount > 0)
			{
				if( camPos.x  > boundRect_right - boundOffset_x )//+ 0.5f)
					camPos.x  = boundRect_right - boundOffset_x;//+ 0.5f;
			}
			else
				camPos.x = NGUIMath.SpringLerp(camPos.x, boundRect_right - boundOffset_x, boundSpringStrength, deltaTime);
		}
		if(camPos.y < boundRect_bottom + boundOffset_y)
		{
			//camPos.y = boundRect_bottom + boundOffset_y;
			if(Input.GetMouseButton(0) || Input.touchCount > 0)
			{
				if(camPos.y < boundRect_bottom + boundOffset_y )//- 0.52f)
					camPos.y = boundRect_bottom + boundOffset_y;//- 0.52f;
			}
			else
				camPos.y = NGUIMath.SpringLerp(camPos.y, boundRect_bottom + boundOffset_y, boundSpringStrength, deltaTime);
		}
		if(camPos.y > boundRect_top - boundOffset_y)
		{
			//camPos.y = boundRect_top - boundOffset_y;
			if(Input.GetMouseButton(0) || Input.touchCount > 0)
			{
				if(camPos.y > boundRect_top - boundOffset_y )//+ 0.48f)
					camPos.y = boundRect_top - boundOffset_y;//+ 0.48f;
			}
			else
				camPos.y = NGUIMath.SpringLerp(camPos.y, boundRect_top - boundOffset_y, boundSpringStrength, deltaTime);
		}
		transform.position = camPos;	
	}
	
	
	
	float DistBetweenTouches()
	{
		if(Input.touchCount >= 2)
			return (Input.touches[0].position - Input.touches[1].position).magnitude;
		
		return 0;
		
	}
	
	float ZoomOffset(float currentDistSq)
	{
		if(Input.touchCount >= 2)
			return -(currentDistSq - prevDistSq);
		else if(Input.GetAxis("Mouse ScrollWheel") > 0)
			return -10;
		else if(Input.GetAxis("Mouse ScrollWheel") < 0)
			return 10;
		return 0;
	}
	
	Vector2 GetTouchPos()
	{
		if(Input.touchCount >= 1)
			return Input.touches[0].position;
		
		Vector2 pos = new Vector2( Input.mousePosition.x, Input.mousePosition.y);
		return pos;
	}
	
	bool IsMultiTouchMoved()
	{
		if(Input.touchCount < 2)
			return true;
		if(Input.touches[0].phase == TouchPhase.Moved && Input.touches[1].phase == TouchPhase.Moved)
			return true;
		return false;
	}
	
	bool IsMultiTouch()
	{
		if(Input.touchCount >= 2)
			return true;
		else if(Input.GetAxis("Mouse ScrollWheel") != 0)
			return true;
		
		return false;
	}
	
	bool IsTouched()
	{
		if(Input.touchCount >= 1)
		{
			foreach( Rect ignoreTouchRect in ignoreTouchRectList)
			{
				if( ignoreTouchRect.Contains( Input.touches[0].position) )
				{
					return false;
				}
			}
			return true;
		}
		else if(Input.GetMouseButton(0))
		{
			foreach( Rect ignoreTouchRect in ignoreTouchRectList)
			{
				if( ignoreTouchRect.Contains( Input.mousePosition) )
				{
					return false;
				}
			}			
			return true;
		}
		else if(Input.GetAxis("Mouse ScrollWheel") != 0)
			return true;
		return false;
	}
	
	bool IsTouchMoved()
	{
		if(Input.touchCount == 0)
			return true;
		if(Input.touches[0].phase == TouchPhase.Moved)
			return true;
		return false;
	}

}
